package com.locpx.lct;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JFileChooser;

@SuppressWarnings("serial")
public class LCTFileChooser extends JFileChooser {
	private static LCTFileChooser instance;
	
	private String fileType = LCTFileFilter.FILE_TYPE_PGN;
	
	private LCTFileFilter pgnFilter;
	private LCTFileFilter jpgFilter;
	
	private LCTFileChooser() {
		super();
		setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		setAcceptAllFileFilterUsed(false);
		
		pgnFilter = new LCTFileFilter(".png", "Portable Network Graphics (*.pgn)");
		jpgFilter = new LCTFileFilter(".jpg", "JPEG (*.jpg)");
		
	    addChoosableFileFilter(pgnFilter);
	    addChoosableFileFilter(jpgFilter);
	    
	    setFileFilter(pgnFilter);
	    
	    addPropertyChangeListener(JFileChooser.FILE_FILTER_CHANGED_PROPERTY, new PropertyChangeListener() {
			
			@Override
			public void propertyChange(PropertyChangeEvent event) {
				if (getFileFilter() == pgnFilter) {
					fileType = LCTFileFilter.FILE_TYPE_PGN;
				} else if (getFileFilter() == jpgFilter) {
					fileType = LCTFileFilter.FILE_TYPE_JPG;
				}
			}
		});
	}
	
	public static LCTFileChooser getInstance() {
		if (instance == null) {
			instance = new LCTFileChooser();
		}
		
		return instance;
	}

	public String getFileType() {
		return fileType;
	}
	
}
